import { setToken,getToken } from "@/utils"
import {defineStore} from 'pinia'
import request from "@/utils/request"
import router from "@/router"

export const useUser = defineStore('useUser', {
    state: () => ({
      userName: '',     
      userId: 0,
      hasGetInfo:false,
      access: [],
      sceneAccess:'',
      sceneDate:'',
      specialpay:'',
      scene_only_query:'',
      querywharf:'',
      scene_only_search:0,
      token: getToken()     
    }),
    getters: {
  
    },
    actions: {
      setToken(token) {
        setToken(token)
      },
      handleLogin(userName, password) {
        return new Promise((resolve, reject) => {
          const params={
            url:'/scene/login/login_by_user',
            method:"POST",
            data:{
              userName,
              password
            }
          }
         
          request(params).then(res => {            
            if (res.status == true) {
              setToken(res.data.token)                   
              this.token = res.data.token
              this.getUserInfo()
              resolve(res)
            } else {
              setToken('')
              resolve(res)
            }
          }).catch(err => {
            reject(err)
          })
        })
  
      },
      handleLogout() {       
        setToken('')
        router.push({
          name: 'login'
        })
      },
      getUserInfo() {
        return new Promise((resolve, reject) => {
          const params = {
            url:'/scene/info/show',
            method:"POST",
            data:{
  
            }
          }
          request(params).then(res => {
            if (res.status == true) {           
              this.userId = Number(res.data.id)           
              this.hasGetInfo=true
              this.userName = res.data.username
              this.sceneAccess = res.data.sceneorder
              this.sceneDate = res.data.scenedate
              this.specialpay = res.data.specialpay
              this.scene_only_query=res.data.scene_only_query
              this.querywharf = res.data.querywharf
              this.scene_only_search = res.data.scene_only_search
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(err=>{
            reject(err)
          })
        })

        
      }
    }
  })